/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spakehelpdesk.bean;

import com.spakehelpdesk.entity.Account;
import com.spakehelpdesk.entity.Category;
import com.spakehelpdesk.entity.Complaint;
import com.spakehelpdesk.entity.Priority;
import com.spakehelpdesk.entity.Status;
import com.spakehelpdesk.java.MailConfig;
import com.spakehelpdesk.java.MessageManage;
import com.spakehelpdesk.java.SessionManage;
import com.spakehelpdesk.java.URLManage;
import com.spakehelpdesk.session.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author leehoanh
 */
@ SessionScoped
public class ComplaintBean implements Serializable {

    @EJB
    private StatusSession statusSession;
    @EJB
    private PrioritySession prioritySession;
    @EJB
    private CategorySession categorySession;
    @EJB
    private AccountSession accountSession;
    @EJB
    private ComplaintSession complaintSession;
    private List<Complaint> listComplaint;
    private List<Complaint> listSearchComplaint;
    private Complaint newComplaint = new Complaint();
    private Complaint selectComplaint;
    private int priorityId = 1;
    private String statusId = "";
    private String technicainId = "";
    private SelectItem[] listPriority = null;
    private SelectItem[] listStatus = null;
    private SelectItem[] listTechinicaint = null;
    private SelectItem[] listCategory = null;
    private Complaint selectComplaintSearch;
    private Category selectCategory;
    private Account listaccount;

    public Category getSelectCategory() {
        return selectCategory;
    }

    public void setSelectCategory(Category selectCategory) {
        this.selectCategory = selectCategory;
    }

    public AccountSession getAccountSession() {
        return accountSession;
    }

    public void setAccountSession(AccountSession accountSession) {
        this.accountSession = accountSession;
    }

    public Complaint getSelectComplaintSearch() {
        return selectComplaintSearch;
    }

    public void setSelectComplaintSearch(Complaint selectComplaintSearch) {
        this.selectComplaintSearch = selectComplaintSearch;
    }

    public ComplaintSession getComplaintSession() {
        return complaintSession;
    }

    //get category name by category id
    public SelectItem[] getListCategory() {
        List<Category> category = categorySession.getAll();
        listCategory = new SelectItem[category.size() + 1];
        if (category != null) {
            listCategory[0] = new SelectItem("", "--Select--");
            for (int i = 0; i < category.size(); i++) {
                listCategory[i + 1] = new SelectItem(category.get(i).getCategoryID(), category.get(i).getSymbol() + " : " + category.get(i).getCategoryName());

            }
        }
        return listCategory;
    }

    public void setListCategory(SelectItem[] listCategory) {
        this.listCategory = listCategory;
    }

    public void setComplaintSession(ComplaintSession complaintSession) {
        this.complaintSession = complaintSession;
    }

    public List<Complaint> getListComplaint() {
        return listComplaint;
    }

    public void setListComplaint(List<Complaint> listComplaint) {
        this.listComplaint = listComplaint;
    }
    //get priority name by priority id

    public SelectItem[] getListPriority() {
        List<Priority> priority = prioritySession.getAll();
        listPriority = new SelectItem[priority.size() + 1];
        if (priority != null) {
            listPriority[0] = new SelectItem("", "--Select--");
            for (int i = 0; i < priority.size(); i++) {
                listPriority[i + 1] = new SelectItem(priority.get(i).getPriorityID(), priority.get(i).getPriorityName());
            }
        }
        return listPriority;
    }

    public Account getListaccount() {
        return listaccount;
    }

    public void setListaccount(Account listaccount) {
        this.listaccount = listaccount;
    }

    public void setListPriority(SelectItem[] listPriority) {
        this.listPriority = listPriority;
    }

    public List<Complaint> getListSearchComplaint() {
        return listSearchComplaint;
    }

    public void setListSearchComplaint(List<Complaint> listSearchComplaint) {
        this.listSearchComplaint = listSearchComplaint;
    }

    //get status name by status id
    public SelectItem[] getListStatus() {
        List<Status> status = statusSession.getAll();
        listStatus = new SelectItem[status.size() + 1];
        if (status != null) {
            listStatus[0] = new SelectItem("", "--Select--");
            for (int i = 0; i < status.size(); i++) {
                listStatus[i + 1] = new SelectItem(status.get(i).getStatusId(), status.get(i).getStatusName());
            }
        }
        return listStatus;
    }

    public void setListStatus(SelectItem[] listStatus) {
        this.listStatus = listStatus;
    }

    //get technicaint name by account id
    public SelectItem[] getListTechinicaint() {
        List<Account> technicain = accountSession.getAccountByRole(new Integer(3));
        listTechinicaint = new SelectItem[technicain.size() + 1];
        if (technicain.size() > 0) {
            listTechinicaint[0] = new SelectItem("", " --Select-- ");
            for (int i = 0; i < technicain.size(); i++) {
                listTechinicaint[i + 1] = new SelectItem(technicain.get(i).getAccountID(), technicain.get(i).getFullName());

            }

        }
        return listTechinicaint;
    }

    public void setListTechinicaint(SelectItem[] listTechinicaint) {
        this.listTechinicaint = listTechinicaint;
    }

    public Complaint getNewComplaint() {
        return newComplaint;
    }

    public void setNewComplaint(Complaint newComplaint) {
        this.newComplaint = newComplaint;
    }

    public int getPriorityId() {
        return priorityId;
    }

    public void setPriorityId(int priorityId) {
        this.priorityId = priorityId;
    }

    public Complaint getSelectComplaint() {
        return selectComplaint;
    }

    public void setSelectComplaint(Complaint selectComplaint) {
        this.selectComplaint = selectComplaint;
    }

    public String getStatusId() {
        return statusId;
    }

    public void setStatusId(String statusId) {
        this.statusId = statusId;
    }

    public String getTechnicainId() {
        return technicainId;
    }

    public void setTechnicainId(String technicainId) {
        this.technicainId = technicainId;
    }

    //get all information by account id
    public List<Complaint> getAllByAccountID() {
        listComplaint = complaintSession.getComplaintByAccount(SessionManage.getValue("accountId").toString());
        return listComplaint;
    }

    //redirect view complaint
    public void viewComplaint() {
        URLManage.sendRediret("Admin/ViewComplaint.xhtml");
    }

    public void viewComplaintSearch() {
        selectComplaint = selectComplaintSearch;
        URLManage.sendRediret("Admin/ViewComplaint.xhtml");
    }

    //update complaint
    public void update() {
        selectComplaint.setPriorityID(new Priority(priorityId));
        selectComplaint.setTechnicianID(new Account(technicainId));
        selectComplaint = complaintSession.updateComplaint(selectComplaint);
    }

    //get all complaint
    public List<Complaint> getAllComplaints() {
        if (SessionManage.getValue("loadcomplaint") == null) {
            listComplaint = complaintSession.getAll();
            SessionManage.setValue("loadcomplaint", "loaded");
        }
        return listComplaint;
    }

    //search all complaint
    public List<Complaint> getAllComplaintsforSearch() {
        listSearchComplaint = complaintSession.getAll();
        return listSearchComplaint;
    }

    //back navigation
    public void back() {
        if (SessionManage.getValue("loadcomplaint") != null) {
            SessionManage.remove("loadcomplaint");
        }

        URLManage.sendRediret("Admin/ManageComplaint.xhtml");


    }

    public Category getCategorySelect(int categoryID) {
        selectCategory = categorySession.getCategoryById(categoryID);
        return selectCategory;
    }

    //delete complaint
    public void delete() {
        complaintSession.remove(selectComplaint);
        if (SessionManage.getValue("loadcomplaint") != null) {
            SessionManage.remove("loadcomplaint");
        }
    }

    /**
     * Creates a new instance of ComplaintBean
     */
    public ComplaintBean() {
    }

    //Resend
    public void resend() {
        Date date1 = new Date();
        Date date2 = selectComplaint.getDateLodge();
        System.out.println("************************" + date2);
        int longdate = date1.compareTo(date2);
        System.out.println("************************" + longdate);
        if (longdate >= 1 && selectComplaint.getStatus().getStatusId().equals("PEND")) {
            try {
                MailConfig.send("smtp.gmail.com", "congsonit@gmail.com", "vipconsion.kiemsino.09clc@gmail.com", "09050027991", selectComplaint.getNote().toString(), selectComplaint.getDescription().toString());
                int count = selectComplaint.getSendTimes().intValue() + 1;
                selectComplaint.setSendTimes(Integer.valueOf(count));
                complaintSession.updateComplaint(selectComplaint);
                MessageManage.message(FacesMessage.SEVERITY_INFO, " RESEND MAIL SUCCESSFULLY", "Resend successfull !");
            } catch (Exception ex) {
                Logger.getLogger(ComplaintBean.class.getName()).log(Level.SEVERE, null, ex);
                MessageManage.message(FacesMessage.SEVERITY_ERROR, " RESEND MAIL FAIRED", "Resend faired !");
            }

        } else {
            MessageManage.message(FacesMessage.SEVERITY_ERROR, " RESEND MAIL FAIRED", "Resend faired !");
        }
    }
    
    public void toCreateCom(){
        URLManage.sendRediret("Employee/ViewTypeComplaint.xhtml");
    }
    
    public void toViewComplaint(){
        URLManage.sendRediret("Employee/ViewComplaint.xhtml");
    }
}
